package com.scalar.chiptrack.engineering.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.poi.ss.usermodel.Workbook;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.ExcelColumnProperties;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.utils.ExcelUtils;
import com.scalar.chiptrack.engineering.forms.GraphReportForm;
import com.scalar.chiptrack.engineering.dao.EngineeringDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.reports.DataDumpReportsHelper;


public class GraphReportAction  extends ChipTrackExtendedAction
{   private static final String ACTION_NAME = "/graphReport.do";
    public ActionForward executeRequest(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        String action = request.getParameter("action");
        HttpSession session = request.getSession();
        GraphReportForm graphReportForm = ( GraphReportForm )form;
        HashMap<String,String> info = new HashMap<String,String>();
        HashMap<String,String> errors = new HashMap<String,String>();

        Workbook workbook = null;
        String fileName = "";

        request.setAttribute( SessionKeys.INFO_MESSAGES, info );
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );

        User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );

        if( ( action == null ) || ( action.trim().length() == 0 ) )
        {
            action = "cycleTimeReport";
        }
        else
        {
            action = action.trim();
        }

        if ( user == null )
        {
            session.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME + "?" + request.getQueryString() );
            return ( mapping.findForward ( "login" ) );
        }
        try
        {
            if ( "cycleTimeReport".equalsIgnoreCase( action ) )
            {
                if( ( graphReportForm.getProcessType() == null ) || "0".equalsIgnoreCase(graphReportForm.getProcessType() ) )
                {
                    request.setAttribute( "partNumberList", new ArrayList() );
                }
                else
                {

                    if ( graphReportForm.getBaseProductId() > 0  )
                    {
                        request.setAttribute( "partNumberList", ProductDao.getPartNumberByBaseProductAndPsTypeList(graphReportForm.getProcessType() , graphReportForm.getBaseProductId() ) );
                    }
                    else
                    {
                        request.setAttribute( "partNumberList", ProductDao.loadAllPartsForProcessTypeByPsType( graphReportForm.getProcessType() ) );
                    }
                }
                request.setAttribute( "processList",        NewProductDAO.loadWipProcessesFromDB( null ,false) );
                request.setAttribute( "baseProductList",    ProductDao.loadChipBaseProducts(null) );
                request.setAttribute( "vendorList",    VendorDAO.getVendorNamesByPsType(null, "all" ));
                return  mapping.findForward( "cycleTimeReport" );
            }
            else if ("cycleTimeXlsReport".equalsIgnoreCase(action) )
            {
                ExcelColumnProperties colProps1 = new ExcelColumnProperties("Actual Yield");
                colProps1.setCellStyle(ExcelColumnProperties.CELL_STYLE_FLOAT_PER);
                ExcelColumnProperties colProps2 = new ExcelColumnProperties("Std Yield");
                colProps2.setCellStyle(ExcelColumnProperties.CELL_STYLE_FLOAT_PER);

                List<ExcelColumnProperties> list = new ArrayList<ExcelColumnProperties>();
                list.add(colProps1);list.add(colProps2);

                ArrayList dataList = EngineeringDAO.getCycleTimeReport(null, graphReportForm);
                workbook =  DataDumpReportsHelper.getWorkbook( dataList, "CylceTime Report", null,null,list );
                fileName = "CylceTime Report_" + DateUtil.getTimeStamp() ;
                ExcelUtils.generateExcelReport( workbook, fileName, response );
            }
            else if ( "yieldGraphReport".equalsIgnoreCase(action) )
            {
                request.setAttribute( "processTypes", NewProductDAO.loadWipProcessesFromDB( null ,false) );
                return mapping.findForward("yieldGraphReport");

            }
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        return null;
    }

}
